package com.boc.ewr.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boc.ewr.bcimp.entity.Pro;
import com.boc.ewr.bcimp.entity.QccBusInfo;
import com.boc.ewr.bcimp.entity.QccBusInfo;
import com.boc.ewr.bcimp.service.IProService;
import com.boc.ewr.bcimp.service.IQccBusInfoService;
import com.boc.ewr.risk.entity.AcctRvkded;
import com.boc.ewr.risk.mapper.AcctRvkdedMapper;
import com.boc.ewr.risk.service.IAcctRvkdedService;
import com.boc.ewr.util.file.enumeration.CompanyStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 已注吊销账户清单
 * @Author: jeecg-boot
 * @Date: 2023-04-20
 * @Version: V1.0
 */
@Service
public class AcctRvkdedServiceImpl extends ServiceImpl<AcctRvkdedMapper, AcctRvkded> implements IAcctRvkdedService {

    @Autowired
    private IProService proService;

    @Autowired
    private IQccBusInfoService qccBusInfoService;

    /**
     * 每季度月初在导入注吊销客户清单后，
     * 与PRO匹配出已注吊销账户的客户号，自动形成已注吊销账户清单下发。
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createInfo() {
        List<QccBusInfo> qccBusInfoList = qccBusInfoService.list();

        // 获取全部注吊销状态
        List<String> rvkdStaList = new ArrayList<>();
        rvkdStaList.add(CompanyStatusEnum.SHUT1.getIndex());
        rvkdStaList.add(CompanyStatusEnum.BLANK.getIndex());
        rvkdStaList.add(CompanyStatusEnum.SHUT2.getIndex());
        rvkdStaList.add(CompanyStatusEnum.SHUT3.getIndex());
        rvkdStaList.add(CompanyStatusEnum.SHUT4.getIndex());
        rvkdStaList.add(CompanyStatusEnum.SHUT5.getIndex());
        rvkdStaList.add(CompanyStatusEnum.SHUT6.getIndex());
        rvkdStaList.add(CompanyStatusEnum.SHUT7.getIndex());


        List<AcctRvkded> acctRvkdedList = new ArrayList<>();
        for (QccBusInfo qccQryAll : qccBusInfoList) {
            // 判断是否注销
            if (rvkdStaList.contains(qccQryAll.getSignSta())) {

                List<Pro> proList = proService.list(
                        new LambdaQueryWrapper<Pro>()
                                .eq(Pro::getCusmName, qccQryAll.getCoreNm())
                );
                if (proList.size() > 0) {
                    for (Pro pro : proList) {
                        AcctRvkded acctRvkded = new AcctRvkded();
                        acctRvkded.setCusmNm(pro.getCusmName());
                        acctRvkded.setKeepSta(qccQryAll.getSignSta());
                        acctRvkded.setCusmNo(pro.getCusmNo());
                        acctRvkded.setAcctNo(pro.getAcctNo());

                        acctRvkdedList.add(acctRvkded);
                    }
                }
            }
        }

        return this.saveBatch(acctRvkdedList);
    }
}
